
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

import org.fusesource.jansi.AnsiConsole;


public class Reentrada {
	
	public static void entrada() {
		AnsiConsole.out.print("\u001b[2J");
		Scanner dados = new Scanner(System.in);
		short x=1, erro1=1;
		long bolsa;
		System.out.println("___________________________________Hemouna______________________________________");
		System.out.println("_____________________________Reentrada de Bolsas________________________________");
		System.out.println("\n\n\n");
		try{
			Class.forName("org.hsqldb.jdbcDriver" ); 	    
			Connection con = DriverManager.getConnection("jdbc:hsqldb:file:Banco/BD", "sa", "");
			Statement stm = con.createStatement(); 
			while (x == 1){
				System.out.print("Numero da bolsa: ");
				while (erro1 == 1){
					erro1 =2;
					bolsa = dados.nextLong();
					ResultSet rs = stm.executeQuery("SELECT id FROM bolsadesangue where id ="+bolsa);
					dados.nextLine();
					while (rs.next()){
						int id = rs.getInt("id");
						if (id == bolsa){
							stm.execute("UPDATE bolsadesangue SET saida = 0 where id = "+bolsa);
							erro1=3;
							break;
						}
						if (erro1 == 2){
							System.out.println("Numero da bolsa incorreto. Informe outro.");
							erro1=1;
						}
					}
				}
				
				System.out.printf("\n+------------------------------------------------------------------------------+");
				System.out.printf("|                          Bolsa liberada no estoque                           |");
				System.out.printf("+------------------------------------------------------------------------------+");
				System.out.print("\n\nInformar outra bolsa? 1-Sim ou 2-Nao\n\n");
				x = dados.nextShort();
				dados.nextLine();
			}
		stm.execute("SHUTDOWN");
		}catch(ClassNotFoundException e){
		    System.out.println("Erro ao carregar o driver JDBC. ");
		}catch(Exception e){
		    System.out.println("Erro de SQL: "+e);
		    e.printStackTrace();
		}
		MenuPrincipal.main(null);

	}

}
